package StackTest;

import java.util.Arrays;

public class MyStack {
    //使用数组实现
    public int[] elem;
    public int usedSize;

    public MyStack() {
        this.elem = new int[10];
    }

    //push 压栈
    public void  push(int val){
        if(isFull()){
            //扩容
            elem = Arrays.copyOf(elem,2*elem.length);
        }
        elem[usedSize++] = val;
    }
    //判断是不是满的？
    public boolean isFull(){
        return usedSize == elem.length;
    }

    //pop 出栈
    public int  pop() {
        if(isEmpty()){
            throw new EmptyException("栈是空的！");
        }
        //1. 先减减
        //2. 后返回栈顶元素
        return elem[--usedSize];
    }
    public boolean isEmpty(){
        return usedSize == 0;
    }

    public int  peek() {
        if(isEmpty()){
            throw new EmptyException("栈是空的！");
        }
        //获取栈顶元素
        return elem[usedSize-1];
    }




}
